package com.chao.config;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chao.model.entity.User;
import com.chao.service.UserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * @Author:PengChao
 * @Description:
 * @Date: Created in 10:31 2021/7/30
 * @Modified By:
 */
@Component
public class MyUserDetail implements UserDetailsService {

    @Resource
    private UserService userService;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User one = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, s));

        UserGrantedAuthority authority = new UserGrantedAuthority("username",s);
        JSONArray array = new JSONArray();
        array.add("a");
        array.add("b");
        array.add("c");
        UserGrantedAuthority interfaces = new UserGrantedAuthority("interfaces",array);

        return new org.springframework.security.core.userdetails.User(one.getUsername(),one.getPassword(), Arrays.asList(authority,interfaces));
    }
}
